package Weapon

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	cs_message "sanguo/protocol/cs/message"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionEquipWeapon struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.EquipWeaponToC
	errcode cs_message.EnumType
}

func (this *transactionEquipWeapon) GetModuleName() string {
	return "Weapon"
}

func (this *transactionEquipWeapon) GetReplyMsg() proto.Message {
	return &cs_message.EquipWeaponToC{}
}

func (this *transactionEquipWeapon) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionEquipWeapon) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionEquipWeapon) Begin() {

	this.resp = &cs_message.EquipWeaponToC{}
	msg := this.req.GetData().(*cs_message.EquipWeaponToS)
	equipPos := msg.GetEquipPos()
	weaponId := msg.GetWeaponId()

	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	role := roleModule.CurrentRole()
	if role == nil || !role.IsRoleModleData() {
		node_game.Infoln(role.GetUserID(), "EquipWeapon fail: role is nil")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}
	node_game.Infof("%s %d Call EquipWeapon id:%d pos:%d ", role.GetUserID(), role.GetID(), msg.GetWeaponId(), msg.GetEquipPos())

	if equipPos < 1 || equipPos > 2 {
		node_game.Infoln(role.GetUserID(), role.GetID(), "EquipWeapon fail:weapon equipPos fail:", equipPos)
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	weapon := role.GetRoleWeapon().GetWeapon(weaponId)
	if weapon == nil {
		node_game.Infoln(role.GetUserID(), role.GetID(), "EquipWeapon fail:Weapon is nil")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	//判断当前武器是否已经装备
	if weapon.GetWeaponState() != 0 {
		if weapon.GetWeaponState() == equipPos {
			node_game.Infoln(role.GetUserID(), role.GetID(), "EquipWeapon OK")
			this.errcode = cs_message.EnumType_OK
			this.user.EndTrans(this)
			return
		} else {
			node_game.Infoln(role.GetUserID(), role.GetID(), "The Weapon is equiped pos:", weapon.GetWeaponState())
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}
	}

	//装备武器
	if !role.GetRoleWeapon().EquipWeapon(weaponId, equipPos) {
		node_game.Infoln(role.GetUserID(), role.GetID(), "EquipWeapon fail")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	//change roleData weapon
	if equipPos == 1 {
		role.SetWeapon1(weaponId)

	} else if equipPos == 2 {
		role.SetWeapon2(weaponId)
	}

	//将外观传到map
	role.ChangeOutline()

	//重新计算战斗属性
	role.TotalBattleAttr()

	//发送背包变化
	role.OnWeaponDirty()

	node_game.Infoln(role.GetUserID(), role.GetID(), "EquipWeapon OK")
	this.errcode = cs_message.EnumType_OK
	this.user.EndTrans(this)
}

func (this *transactionEquipWeapon) End() {
	this.resp.Errcode = cs_message.EnumType(this.errcode).Enum()
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionEquipWeapon) Timeout() {
	this.resp.Errcode = cs_message.EnumType(cs_message.EnumType_RETRY).Enum()
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.EquipWeaponToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionEquipWeapon{
			user: user,
			req:  msg,
		}
	})
}
